#!/usr/bin/env python
#-*- coding:utf-8 -*-

# token.py
#
# Copyright (c) 2007 Marcelo Lira dos Santos
#
# Author: Marcelo Lira dos Santos <setanta@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public License
# as published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA

import pygtk
pygtk.require('2.0')
import gtk
import gobject
import cairo
import math

from piemenu import PieMenu

class Token(gobject.GObject):

    RADIUS = 16

    def __init__(self, pos=(0,0), imagefile=None, menu=None, canvas=None):
        gobject.GObject.__init__(self)
        self.pos = None
        self.canvas = canvas
        self.menu = menu
        self.image = None

        self.set_pos(pos)
        if imagefile:
            self.set_image(imagefile)


    def set_pos(self, pos=(0,0)):
        self.pos = pos
        if self.menu:
            self.menu.set_pos(pos)


    def set_canvas(self, canvas):
        self.canvas = canvas
        if self.menu:
            self.menu.set_canvas(canvas)


    def set_image(self, imagefile):
        image = cairo.ImageSurface.create_from_png(imagefile)
        self.image = image


    def set_menu(self, menu):
        menu.set_canvas(self.canvas)
        menu.set_pos(self.pos)
        self.menu = menu


    def get_menu(self):
        return self.menu


    def get_pos(self):
        return self.pos


    def is_mouse_over(self, pos):
        dist = math.hypot(abs(self.pos[0] - pos[0]), abs(self.pos[1] - pos[1]))
        return dist < Token.RADIUS


    def button_release(self):
        pass


    def draw(self, context):
        x, y = self.pos
        x -= self.image.get_width() / 2
        y -= self.image.get_height() / 2

        context.set_source_surface(self.image, x, y)
        context.paint()

